package com.party.admin.web.controller.audio;

import com.party.admin.web.dto.AjaxResult;
import com.party.admin.web.dto.input.common.CommonInput;
import com.party.common.constant.Constant;
import com.party.common.paging.Page;
import com.party.common.utils.StringUtils;
import com.party.core.model.BaseModel;
import com.party.core.model.audio.Audio;
import com.party.core.model.crowdfund.CrowdfundEvent;
import com.party.core.service.audio.IAudioService;
import com.party.core.service.crowdfund.ICrowdfundEventService;
import com.party.core.utils.MyBeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * @program: party
 * @description: 音频控制器
 * @author: Hua Qunhao
 * @create: 2019-03-11 17:00
 **/
@Controller
@RequestMapping(value = "/audio")
public class AudioController {
    protected static Logger logger = LoggerFactory.getLogger(AudioController.class);

    @Autowired
    private IAudioService audioService;

    @Autowired
    private ICrowdfundEventService crowdfundEventService;

    /**
     * 音频管理
     *
     * @param audio
     * @param page
     * @param commonInput
     * @return
     */
    @RequestMapping(value = "/audioList")
    public ModelAndView audioList(Audio audio, Page page, CommonInput commonInput) {
        ModelAndView mv = new ModelAndView("audio/audioList");

        audio.setDelFlag(BaseModel.DEL_FLAG_NORMAL);
        Map<String, Object> params = CommonInput.appendParams(commonInput);

        List<Audio> audioList = audioService.webListPage(audio, params, page);

        mv.addObject("input", commonInput);
        mv.addObject("audio", audio);
        mv.addObject("audioList", audioList);
        mv.addObject("page", page);
        return mv;
    }

    /**
     * 表单
     *
     * @param id
     * @return
     */
    @RequestMapping("/toForm")
    public ModelAndView toForm(String id) {
        ModelAndView mv = new ModelAndView("audio/audioForm");
        if (StringUtils.isNotEmpty(id)) {
            Audio audio = audioService.get(id);
            mv.addObject("audio", audio);
        }
        return mv;
    }

    /**
     * 新增/编辑
     *
     * @param audio
     * @param result
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public AjaxResult save(Audio audio, BindingResult result) {
        // 数据验证
        if (result.hasErrors()) {
            List<ObjectError> allErros = result.getAllErrors();
            return AjaxResult.error(allErros.get(0).getDefaultMessage());
        }
        try {

            if (StringUtils.isEmpty(audio.getId())) {
                audio.setAudioType(Constant.IS_CROWDFUNDED);
                audioService.insert(audio);
            } else {
                Audio dbaudio = audioService.get(audio.getId());
                MyBeanUtils.copyBeanNotNull2Bean(audio, dbaudio);
                audioService.update(dbaudio);
            }
            return AjaxResult.success();
        } catch (Exception e) {
            logger.error("音频保存异常", e);
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete")
    public AjaxResult delete(String id) {
        if (StringUtils.isEmpty(id)) {
            return AjaxResult.error("主键不能为空");
        }
        CrowdfundEvent crowdfundEvent = new CrowdfundEvent();
        crowdfundEvent.setIsPlayMusic(Constant.IS_CROWDFUNDED);
        crowdfundEvent.setAudioId(id);
        List<CrowdfundEvent> CrowdfundEventlist = crowdfundEventService.findEventByAudio(crowdfundEvent);

        if (CrowdfundEventlist.size() == 0) {
            audioService.delete(id);
            return AjaxResult.success();
        } else {
            return AjaxResult.error("音频已被使用不能直接删除");
        }
    }

    /**
     * 校验音频名称
     *
     * @param audioTitle
     * @return
     */
    @ResponseBody
    @RequestMapping("/verifyAudioTitle")
    public AjaxResult verifyAudioTitle(String audioTitle, String id) {
        if (StringUtils.isEmpty(audioTitle)) {
            return AjaxResult.error("音频名称不能为空");
        }
        Audio audio = new Audio();
        audio.setId(id);
        audio.setAudioTitle(audioTitle);
        List<Audio> audioList = audioService.findAudio(audio);
        if (audioList.size() == 0) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error("音频名称已存在");
        }
    }

    /**
     * 获取音频列表
     *
     * @return AjaxResult
     */
    @ResponseBody
    @RequestMapping("/getAudioListAll")
    public AjaxResult getAudioListAll() {
        List<Audio> audioList = null;
        String message = "音频列表为空";
        try {
            audioList = audioService.list(new Audio(null, Constant.IS_CROWDFUNDED,null));
        } catch (Exception e) {
            message = e.getMessage();
            logger.info("获取音频列表失败", message);
        }
        if (audioList == null || audioList.size() == 0) {
            return AjaxResult.error(message);
        }
        return AjaxResult.success(audioList);
    }
}
